<?php
namespace app\forum_admin\controller;

use app\BaseController;
use think\facade\Config;
use think\facade\Env;
use think\facade\Db;

class Bindeal extends BaseController
{
	use \liliuwei\think\Jump;
	// 检查登录状态
	public function check()
	{
		// 如果没有登录，就做出限制
		if (!session('?aNick')) {
			$this->error("请登录后在访问该功能！","admin/alogin");
		}
	}

	// 查询版块的函数
	public function showSec()
	{
		// 查询版块
		$sec = Db::name("section")
      ->where('s_bin',0)
			->field("s_name,s_id")
			->select();
		return $sec;
	}

	// 检查sid的方法
	public function checkSid($sid){
		// 编写链式操作，查询sid是否合法
		$secYes = Db::name("section")
				->where("s_id",$sid)
				->field("s_name")
				->find();
		// 单分支，检查查询结果是否为null，提示跳转
		// 返回版块名字
		return($secYes["s_name"]);
	}

	// 版块回收站管理
	public function binSecDeal()
	{
    $this->check();
		$sid=input('s_id');
		if ($sid!=-1) {
			$mid=Db::name('mes')
				->where('s_id',$sid)
				->field('m_id')
				->select();
			$mesNum=count($mid);
			for ($i=0; $i < $mesNum; $i++) { 
				$resDel=Db::name("res")
					   ->where('m_id',$mid[$i]['m_id'])
					   ->update(['s_bin'=>1]);
				$resDelat = Db::name("res")
                  ->where('r_bin&m_bin',0)
                  ->where("m_id",$mid[$i]["m_id"])
                  ->update([
                    'm_bin'=>1,
                    'r_bin'=>1,
                    'r_delat'=>time(),
                    'r_reason'=>'删除版块'
                  ]);
			}
			$mesDel = Db::name('mes')
              ->where('s_id',$sid)
              ->update(['s_bin'=>1]);
      $mesDelat = Db::name('mes')
                ->where('s_id',$sid)
                ->where('m_bin',0)
                ->update([
                    'm_bin'=>1,
                    'm_delat'=>time(),
                    'm_reason'=>'删除版块'
                  ]);
      $secDel = Db::name('section')
              ->where('s_id',$sid)
              ->update([
                  's_bin'=>1,
                  's_delat'=>time(),
                  's_reason'=>'删除版块'
                ]);
      if ($secDel!=0) {
            $this->success("版块放入回收站成功!",'bindeal/binSecDeal');
        }
		}else{
        	$this->error("版块放入回收站失败!");
        }
		$find=input('find');
    if ($find!=null) {
        $sec=Db::name("section")
        	->where('s_bin',1)
            ->field('s_id,s_name,s_remark,s_bin,s_delat')
            ->whereLike('s_name',"%$find%")
            ->paginate(10);
    }else{
        $sec=Db::name("section")
        	->where('s_bin',1)
            ->field('s_id,s_name,s_remark,s_bin,s_delat')
            ->paginate(10);
    }
    $num=1;
    $page=$sec->render();

    return view('',['sec'=>$sec,'page' => $page,'num'=>$num]);
	}

	// 版块还原
	public function binSecBack()
	{
    $this->check();
		$sid=input('s_id');
		$sec=Db::name('section')
			->where('s_id',$sid)
			->where('s_bin',1)
			->update([
				's_bin'=>0,
				's_delat'=>0,
        's_reason'=>'恢复版块'
			]);
    $mesBin = Db::name('mes')
            ->where('s_id',$sid)
            ->update([
              's_bin'=>0,
              'm_bin'=>0
            ]);
    $mesDelatbin = Db::name('mes')
            ->where('m_bin',0)
            ->where('s_id',$sid)
            ->update([
              'm_delat'=>0,
              'm_reason'=>'恢复版块'
            ]);
    $mid = Db::name('mes')
        ->where('s_id',$sid)
        ->field('m_id')
        ->select();
    $mesnum=count($mid);
    for($i=0;$i<$mesnum;$i++){
          $resbin = Db::name("res")
                  ->where("m_id",$mid[$i]["m_id"])
                  ->where('s_bin',1)
                  ->update([
                    's_bin'=>0,
                    'm_bin'=>0,
                    'r_bin'=>0
                  ]);
          $resdelatbin = Db::name("res")
                  ->where("m_id",$mid[$i]["m_id"])
                  ->update([
                    'r_delat'=>0,
                    'r_reason'=>'恢复版块'
                  ]);
      }
		if ($sec!=0) {
        	$this->success("版块还原成功!",'deal/SecDeal');
        }else{
        	$this->error("版块还原失败!");
        }
	}

	// 帖子回收站管理
	public function binMesDeal($sid = 1)
	{
    $this->check();
		$sec = Db::name("section")
      ->field("s_name,s_id")
      ->select();
		// 加载某个版块的帖子
		// 检查sid的值
		$thisSec = $this->checkSid($sid);

		$find = input('find');
		if ($sid==0 and $find==null) {
        // 加载全部帖子
      	//依照m_id升序的板块帖子列表
        // 多表查询mes、user
        $mes= Db::view('mes','m_id,m_title,u_nick,m_createat,m_content,m_delat,s_bin,m_reason,m_bin')
            ->view('user',"u_img","mes.u_nick=user.u_nick")
            ->where('m_bin',1)
            ->order('m_id','esc')
            ->order('m_delat')
            ->paginate([
                  'list_rows'=> 10,
                   'query'=>['sid'=>$sid]
            ]);
        //单独赋值分页输出的模板变量
           $page=$mes->render();
         }
         elseif ($find!=null) {
            $mes= Db::view('mes','m_id,m_title,u_nick,m_createat,m_content,m_delat,s_bin,m_reason,m_bin')
                ->view('user',"u_img","mes.u_nick=user.u_nick")
                ->where('m_bin',1)
                ->whereLike('m_title',"%$find%")
                -> order('m_id','esc')
                -> order('m_delat')
                ->paginate([
                  'list_rows'=> 10,
                   'query'=>['sid'=>$sid]
                ]);
        	//单独赋值分页输出的模板变量
            $page=$mes->render();
         }
         else{
          //检查sid，如果是错误sid则提示错误
          $this->checkSid($sid);

        // 加载某个帖子
      	//依照m_id升序的板块帖子列表
        // 多表查询mes、user
           $mes= Db::view('mes','m_id,m_title,u_nick,m_createat,m_content,m_delat,s_bin,m_reason,m_bin')
              ->view('user',"u_img","mes.u_nick=user.u_nick")
              ->where('m_bin',1)
              ->where('s_id',"=",$sid)
              -> order('m_id','esc')
              -> order('m_delat')
              ->paginate([
                  'list_rows'=> 10,
                   'query'=>['sid'=>$sid]
             ]);
           //单独赋值分页输出的模板变量
           $page=$mes->render();
           $sname=Db::name('section')
        		->field('s_name')
        		->where('s_id',$sid)
        		->value('s_name');
         }
    $num=1;

    $mid=input('mid');

		if ($mid!=-1) {

      $resDelat = Db::name('res')
              ->where('m_id',$mid)
              ->where('r_bin',0)
              ->update([
                'm_bin'=>1,
                'r_bin'=>1,
                'r_delat'=>time(),
                'r_reason'=>'删除帖子'
              ]);

      $mesDel = Db::name('mes')
              ->where('m_id',$mid)
              ->update([
              	'm_bin'=>1,
              	'm_delat'=>time(),
              	'm_reason'=>'删除帖子'
              ]);
    if ($mesDel!=0) {
    	$this->success("帖子放入回收站成功!",'bindeal/binMesDeal');
      }
    }else{
			$this->error("帖子放入回收站失败!");
  }
		return view('',['sec'=>$sec,'thisSec'=>$thisSec,'num'=>$num,'page'=>$page,'mes'=>$mes]);
	}

	// 帖子还原
	public function binMesBack()
	{
    $this->check();
		$mid=input('m_id');
		$mes = Db::name('mes')
        ->where('s_bin',0)
        ->where('m_id',$mid)
        ->update(['m_delat'=>0]);
    if ($mes!= 0) {
        $mesDelat=Db::name('mes')
            ->where('m_id',$mid)
            ->update([
              'm_bin'=>0,
              'm_reason'=>'还原帖子'
            ]);
        $resBin = Db::name('res')
            ->where('m_bin',1)
            ->where('m_id',$mid)
            ->update([
              'm_bin'=>0,
              'r_bin'=>0,
              'r_delat'=>0,
              'r_reason'=>'还原帖子'
            ]);
        $this->success("还原成功!",'deal/mesDeal');
    }else{
        $this -> error("还原失败!主管板块已经被放入回收站！");
        exit();
    }
	}

	// 回复回收站管理
	public function binResDeal($sid = 1,$find='%')
	{
    $this->check();
    $sec = Db::name("section")
      ->field("s_name,s_id")
      ->select();
    // 加载某个版块的帖子
    // 检查sid的值
    $thisSec = $this->checkSid($sid);

    $rid=input('r_id');

    if ($rid!=-1) {
      $resDel=Db::name('res')
             ->where('r_id',$rid)
             ->update([
                  'r_bin'=>1,
                  'r_delat'=>time(),
                  'r_reason'=>'删除回复'
              ]);
      if ($resDel!=0) {
        $this->success("回复放入回收站成功!",'bindeal/binResDeal');
      }
    }else{
        $this -> error("回复放入回收站失败!");
    }

    $search='%'.$find.'%';
    $res=Db::view('mes','m_id,m_title')
        ->view('res','r_id,r_content,r_delat,r_createat,u_nick,r_bin,m_bin,s_bin,r_reason','mes.m_id = res.m_id')
        ->where("s_id",$sid)
        ->where('mes.m_title', 'like', $search)
        ->where("r_bin|res.m_bin|res.s_bin",1)
        ->select();

    $num=1;
		return view('',['sec'=>$sec,'thisSec'=>$thisSec,'res'=>$res,'num'=>$num]);
	}

  // 回复还原
  public function binResBack()
  {
    $this->check();
    $rid=input('r_id');
    $res=Db::name('res')
        ->where('m_bin',0)
        ->where('s_bin',0)
        ->where('r_id',$rid)
        ->update([
            'r_bin'=>0,
            'r_delat'=>0,
            'r_reason'=>'还原回复'
        ]);
    if($res!=0){
      $this->success("回复还原成功！");
    }else{
      $this->error("回复还原失败，主管的版块或帖子已经被回收站！");
    }
  }
}